package org.spark.java.transformation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.List;

/**
 * join （K,V）格式的RDD和（K,V）格式的RDD按照key相同join 得到（K,(V,W)）格式的数据,join会产生shuffle
 *
 * @param:
 * @return:
 * @auther: 某人的目光
 * @date: 2020/1/12 23:13
 */
public class Join {

    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf();
        //设置运行环境
        sparkConf.setMaster("local");
        sparkConf.setAppName("join");
        JavaSparkContext context = new JavaSparkContext(sparkConf);

        List<Tuple2<Integer, String>> list1 = Arrays.asList(
                new Tuple2<Integer, String>(1, "某人的目光"),
                new Tuple2<Integer, String>(2, "桜花抄"),
                new Tuple2<Integer, String>(3, "幻听")
        );
        List<Tuple2<Integer, Integer>> list2 = Arrays.asList(
                new Tuple2<Integer, Integer>(1, 99),
                new Tuple2<Integer, Integer>(2, 80),
                new Tuple2<Integer, Integer>(3, 50)
        );
        JavaPairRDD<Integer, String> rdd1 = context.parallelizePairs(list1);
        JavaPairRDD<Integer, Integer> rdd2 = context.parallelizePairs(list2);
        JavaPairRDD<Integer, Tuple2<String, Integer>> join = rdd1.join(rdd2);
        join.foreach(new VoidFunction<Tuple2<Integer, Tuple2<String, Integer>>>() {
            public void call(Tuple2<Integer, Tuple2<String, Integer>> t) throws Exception {
                System.out.println("编号:" + t._1);
                System.out.println("名称:" + t._2._1);
                System.out.println("分数:" + t._2._2);
            }
        });
    }
}
